import AsyncStorage from "@react-native-async-storage/async-storage";

const TOKEN_KEY = "token";

// 获取token
const getToken = (): Promise<string> => {
  return new Promise(async resolve => {
    try {
      const token = await AsyncStorage.getItem(TOKEN_KEY);
      resolve(token || "");
    } catch (error) {
      console.log("token 缓存失效: ", error);
      resolve("");
    }
  });
};

// 设置token
const setToken = (val: string) => {
  return new Promise(async resolve => {
    try {
      await AsyncStorage.setItem(TOKEN_KEY, val);
      resolve(true);
    } catch (error) {
      console.log("token 设置失败: ", error);
      resolve(false);
    }
  });
};

// 删除token
const removeToken = () => {
  return new Promise(async resolve => {
    try {
      await AsyncStorage.removeItem(TOKEN_KEY);
      resolve(true);
    } catch (error) {
      console.log("token 删除失败: ", error);
      resolve(false);
    }
  });
}

export {
  getToken,
  setToken,
  removeToken
}